package cn.v2tila.rsosa.student.controller;

import cn.v2tila.rsosa.student.entity.*;
import cn.v2tila.rsosa.student.jpa.SchoolJPA;
import cn.v2tila.rsosa.student.service.*;
import cn.v2tila.rsosa.student.vo.RequestVO;
import cn.v2tila.rsosa.student.vo.ResultVO;
import cn.v2tila.rsosa.student.vo.StudentInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

/**
 * @author v2tila on 18:29 2019/2/22
 */

@RestController
@RequestMapping(value = "user")
public class userController {
    @Autowired
    private UserService userService;
    @Autowired
    private SchoolService schoolService;
    @Autowired
    private StudentService studentService;
    @Autowired
    private ClassService classService;
    @Autowired
    private CollegeService collegeService;
    @Autowired
    private AssoService assoService;
    @Autowired
    private DeliveryService deliveryService;

    @RequestMapping(value = "login")
    public ResultVO login(RequestVO requestVO){

        String openid = requestVO.getOpenid();

        ResultVO resultVO = new ResultVO();
        System.out.println(requestVO);
        UserEntity userEntity = userService.findByOpenid(openid);
        if(userEntity == null){
            resultVO.setIsUser(0);
        }else{
            resultVO.setIsUser(1);
            resultVO.setUniqueId(userEntity.getUniqueId());
            StudentEntity studentEntity = studentService.findByUniqueId(userEntity.getUniqueId());
            SchoolEntity schoolEntity = schoolService.findByNo(studentEntity.getSchoolNo());
            CollegeEntity collegeEntity = collegeService.findBySchoolNoAndNo(schoolEntity.getNo(),studentEntity.getCollegeNo());
            ClassEntity classEntity = classService.findByNoAndSchoolNoAndCollegeNo(studentEntity.getClassNo(), schoolEntity.getNo(), collegeEntity.getNo());

            StudentInfo studentInfo = new StudentInfo();
            studentInfo.setStudentNo(studentEntity.getNo());
            studentInfo.setStudentName(studentEntity.getName());
            studentInfo.setClassName(classEntity.getName());
            studentInfo.setCollegeName(collegeEntity.getName());
            studentInfo.setSchoolName(schoolEntity.getName());
            studentInfo.setIsAsso(userEntity.getIsAsso());
            resultVO.setStudentInfo(studentInfo);
            if(userEntity.getIsAsso()==1){
                AssoEntity assoEntity = assoService.findByUniqueId(userEntity.getUniqueId());
                resultVO.setHasNewApply(deliveryService.existsByAssoIdAndCode(assoEntity.getId(),0));
                resultVO.setIsPublic(assoEntity.getIsPublic());
            }
        }
        return resultVO;
    }

    @RequestMapping(value = "register")
    public ResultVO register(RequestVO requestVO) {

        ResultVO resultVO = new ResultVO();
        String openid = requestVO.getOpenid();
        String nickName = requestVO.getNickName();
        String avatarUrl = requestVO.getAvatarUrl();
        String schoolName = requestVO.getSchoolName();
        String name = requestVO.getName();
        String stuNo = requestVO.getStuNo();
        String password = requestVO.getPassword();
        String schoolNo;
        SchoolEntity schoolEntity = schoolService.findByName(schoolName);
        if(schoolEntity==null){
            resultVO.setCode(0);
        }else{
            schoolNo = schoolEntity.getNo();
            StudentEntity studentEntity = studentService.findBySchoolNoAndNo(schoolNo, stuNo);
            if (studentEntity == null) {
                resultVO.setCode(0);
            }else{
                if (userService.existsByUniqueId(studentEntity.getUniqueId())){
                    resultVO.setMsg("绑定失败，该学生已绑定微信");
                    resultVO.setCode(0);
                    return resultVO;
                }
                if(studentService.existsBySchoolNoAndNameAndNoAndPassword(
                        schoolNo,name,stuNo,password)){

                    UserEntity userEntity = new UserEntity();
                    userEntity.setOpenid(openid);
                    userEntity.setNickName(nickName);
                    userEntity.setAvatarUrl(avatarUrl);
                    userEntity.setUniqueId(studentEntity.getUniqueId());
                    userEntity.setIsStu(1);
                    userEntity.setIsAsso(0);
                    UserEntity isSuccess = userService.saveAndFlush(userEntity);

                    if (isSuccess == null) {
                        resultVO.setMsg("绑定失败，请联系管理员");
                        resultVO.setCode(0);
                    }else{
                        resultVO.setMsg("绑定成功");
                        resultVO.setCode(1);
                    }

                }else{
                    resultVO.setMsg("绑定失败，学生信息不匹配");
                    resultVO.setCode(0);
                }
            }
        }

        return resultVO;
    }

    @RequestMapping(value = "updateAvatar")
    public ResultVO updateAvatar(String openid, String avatarUrl) {
        ResultVO resultVO = new ResultVO();
        UserEntity userEntity = userService.findByOpenid(openid);
        if(!userEntity.getAvatarUrl().equals(avatarUrl)){
            userEntity.setAvatarUrl(avatarUrl);
            UserEntity saveIsSuccess = userService.saveAndFlush(userEntity);
            if (saveIsSuccess == null) {
                resultVO.setCode(0);
                resultVO.setMsg("请联系管理员");
            }else{
                resultVO.setCode(1);
            }
        }else resultVO.setCode(1);
        return resultVO;
    }

    @RequestMapping(value = "delUser")
    public ResultVO delUser(String uniqueId){
        ResultVO resultVO = new ResultVO();
        try{
            UserEntity userEntity = userService.findByUniqueId(uniqueId);
            if (userEntity==null){
                resultVO.setCode(0);
                resultVO.setMsg("该用户不存在");
            }else{
                if(userEntity.getIsAsso()==1){
                    AssoEntity assoEntity = assoService.findByUniqueId(uniqueId);
                    if(assoEntity!=null){
                        assoEntity.setUniqueId(null);
                        AssoEntity assoSaveIsSuccess = assoService.saveAndFlush(assoEntity);
                        if(assoSaveIsSuccess==null){
                            resultVO.setCode(0);
                            resultVO.setMsg("删除社团管理权限失败，请联系管理员");
                        }
                    }
                }
                userService.deleteUser(userEntity);
                resultVO.setCode(1);
            }
        }catch (Exception e){
            resultVO.setCode(0);
            resultVO.setMsg("删除失败");
        }
        return resultVO;
    }
}
